package com.bearingnavi.android.overlays;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Typeface;

import com.bearingnavi.android.util.ClickableLineOverlay;
import com.bearingnavi.android.util.Mathematics;
import com.bearingnavi.android.views.RotateView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MeasurementOverlay extends Overlay implements ClickableLineOverlay {

	private GeoPoint geoPoint = null;
	private GeoPoint destGeoPoint = null;
	private int LINE_COLOR = Color.GREEN;
	
	public MeasurementOverlay() {
		super();
	}
	
	public void setGeoPoint(GeoPoint geoPoint) {
		this.geoPoint = geoPoint;
	}
	
	public void setDestGeoPoint(GeoPoint destGeoPoint) {
		this.destGeoPoint = destGeoPoint;
	}

	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		super.draw(canvas, mapv, shadow);
		
		if (geoPoint == null || destGeoPoint == null) {
			return;
		}

		Paint paint = new Paint();
		paint.setDither(true);
		paint.setColor(LINE_COLOR);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(5);
		
		Paint roundPaint = new Paint();
		roundPaint.setDither(true);
		roundPaint.setColor(Color.BLUE);
		roundPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		roundPaint.setStrokeJoin(Paint.Join.ROUND);
		roundPaint.setStrokeCap(Paint.Cap.ROUND);
		roundPaint.setStrokeWidth(10);

	    Paint textPaint = new Paint();
	    textPaint.setColor(Color.WHITE);
	    textPaint.setTextAlign(Paint.Align.LEFT);
	    textPaint.setTextSize(26);
	    textPaint.setTypeface(Typeface.DEFAULT_BOLD);
	    textPaint.setShadowLayer(2, 0, 2, Color.GRAY);
	    
	    Paint bgPaint = new Paint();
	    bgPaint.setColor(Color.BLACK);
	    bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
	    bgPaint.setAlpha(75);

	    String balloonText;
	    long distance = Mathematics.calculateDistance(geoPoint, destGeoPoint);
	    if (distance < 1000)
	    	balloonText = distance+"m, ";
	    else
	    	balloonText = (distance/10)/100F +"Km, ";
	    balloonText += Mathematics.calculateInitialBearing(geoPoint, destGeoPoint)+"°";

		Point lastPoint = new Point();
		Point point = new Point();

		Path path = new Path();

		Projection projection = mapv.getProjection();
		projection.toPixels(geoPoint, lastPoint);
		projection.toPixels(destGeoPoint, point);

		path.moveTo(point.x, point.y);
		path.lineTo(lastPoint.x, lastPoint.y);
		
		float textWidth = textPaint.measureText(balloonText);

		canvas.drawPath(path, paint);
		
		canvas.drawPoint(point.x, point.y, roundPaint);
		
		canvas.save();
		
		canvas.rotate(RotateView.getInstance().getBearing(), point.x+20, point.y);
		canvas.drawRect(point.x+10, point.y+10, point.x+30+textWidth, point.y-30, bgPaint);
		canvas.drawText(balloonText, point.x+20, point.y, textPaint);
		
		canvas.restore();
	}

	public GeoPoint getClickablePoint() {
		return geoPoint;
	}
	
	public List<GeoPoint> getLinePoints() {
		List<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
		if (geoPoint != null) {
			geoPoints.add(geoPoint);	
		}
		if (destGeoPoint != null) {
			geoPoints.add(destGeoPoint);
		}
		return geoPoints;
	}

	public void onChosenEvent(MapView mapView) {
		LINE_COLOR = Color.RED;
	}

	public void onReleaseChosenEvent(MapView mapView) {
		LINE_COLOR = Color.GREEN;
	}
	
}
